package com.lonely.fx.bind;

import javafx.beans.binding.ObjectBinding;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleListProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;

/**
 * @date: 2022-07-24 19:10
 * @author: ztkj-hzb
 * @description: valueAt属性的设置
 **/
public class ValueAtSettingMain {

    public static void main(String[] args) {

        ObservableList<String> observableList = FXCollections.observableArrayList("A", "B", "C", "D");
        SimpleListProperty<String> listProperty = new SimpleListProperty<>(observableList);

        //TODO valueAt监听某个索引对应的值
        ObjectBinding<String> valueAt1 = listProperty.valueAt(1);
        System.out.println("listProperty: " + listProperty.get());
        System.out.println("valueAt1: " + valueAt1.get());

        //后面修改了对应索引的值，再次调用valueAt的get方法会得出最新的值
        listProperty.set(0, "A1");
        listProperty.set(1, "A2");
        System.out.println("listProperty: " + listProperty.get());
        System.out.println("valueAt1: " + valueAt1.get());


        //TODO 基于SimpleIntegerProperty作为参数，可以动态修改索引的值
        SimpleIntegerProperty integerProperty = new SimpleIntegerProperty(2);
        ObjectBinding<String> valueAt2 = listProperty.valueAt(integerProperty);
        System.out.println("listProperty: " + listProperty.get());
        System.out.println("valueAt2: " + valueAt2.get());

        listProperty.set(2, "C2");
        System.out.println("listProperty: " + listProperty.get());
        System.out.println("valueAt2: " + valueAt2.get());
    }


}
